package edu.whut.mall.admin.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import edu.whut.mall.admin.domain.UserParams;
import edu.whut.mall.admin.enums.SysStatusEnum;
import edu.whut.mall.admin.enums.UserTypeEnum;
import edu.whut.mall.admin.mapper.UserCustomMapper;
import edu.whut.mall.admin.service.ISysUserService;
import edu.whut.mall.admin.shiro.util.ShiroUtil;
import edu.whut.mall.admin.vo.LoginVo;
import edu.whut.mall.admin.vo.UserVo;
import edu.whut.mall.common.api.CommonPage;
import edu.whut.mall.common.util.Md5Util;
import edu.whut.mall.mapper.InstitutionSysUserMapper;
import edu.whut.mall.mapper.SysUserMapper;
import edu.whut.mall.mapper.SysUserRoleMapper;
import edu.whut.mall.model.InstitutionSysUserExample;
import edu.whut.mall.model.SysUser;
import edu.whut.mall.model.SysUserExample;
import edu.whut.mall.model.SysUserRoleExample;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * @program: d9lab-mall
 * @description: 用户服务实现类
 * @author: Wayne
 * @create: 2020-03-17 13:01
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl implements ISysUserService {

    private final SysUserMapper sysUserMapper;

    private final UserCustomMapper userCustomMapper;

    private final SysUserRoleMapper sysUserRoleMapper;

    private final InstitutionSysUserMapper institutionSysUserMapper;



    @Override
    public SysUser insertOneUser(String RegIp, UserParams userParams) {
        SysUser sysUser = new SysUser();
        BeanUtil.copyProperties(userParams,sysUser);
        sysUser.setStatus(SysStatusEnum.AVAILABLE.getCode());
        sysUser.setPassword(Md5Util.encrypt(userParams.getPassword()));
        sysUser.setRegIp(RegIp);
        int result = sysUserMapper.insertSelective(sysUser);
        if (result >= 0) {
            return sysUser;
        }
        return null;

    }

    @Override
    public LoginVo loginAdmin(String loginIp, String username, String password) {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        SecurityUtils.getSubject().login(token);
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(ShiroUtil.getUserId());
        SysUser newSysUser = new SysUser();
        newSysUser.setId(sysUser.getId());
        newSysUser.setLastLoginIp(loginIp);
        newSysUser.setLastLoginTime(new Date());
        newSysUser.setLoginCount(sysUser.getLoginCount() + 1);
        sysUserMapper.updateByPrimaryKeySelective(newSysUser);
        LoginVo loginVo = new LoginVo();
        loginVo.setToken(ShiroUtil.getSession().getId());
        loginVo.setAdminType(ShiroUtil.getAdminType());
        return loginVo;
    }

    @Override
    public SysUser getUserById(Long id) {
        return sysUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public SysUser getUserByUsername(String username){
        SysUserExample example = new SysUserExample();
        example.createCriteria().andUsernameEqualTo(username);
        List<SysUser> user = sysUserMapper.selectByExample(example);
        return CollectionUtils.isEmpty(user) ? null : user.get(0);
    }

    @Override
    public CommonPage<UserVo> listAdmins(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<UserVo> userVoList = userCustomMapper.listUserVos();
        return CommonPage.restPage(userVoList);
    }

    @Override
    @Transactional
    public Integer deleteOneAdminUser(Long userId) {
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(userId);
        if (UserTypeEnum.INSTITUTION_ADMIN.getCode().equals(sysUser.getUserType())) {
            InstitutionSysUserExample institutionSysUserExample = new InstitutionSysUserExample();
            institutionSysUserExample.createCriteria().andSysUserIdEqualTo(userId);
            institutionSysUserMapper.deleteByExample(institutionSysUserExample);
        }
        sysUserMapper.deleteByPrimaryKey(userId);
        SysUserRoleExample sysUserRoleExample = new SysUserRoleExample();
        sysUserRoleExample.createCriteria().andUserIdEqualTo(userId);
        return sysUserRoleMapper.deleteByExample(sysUserRoleExample);
    }

    @Override
    public Integer deleteAdminsByMerchantId(Long merchantId) {
        return null;
    }

    @Override
    public Integer updateUser(UserParams userParams) {
        SysUser sysUser = new SysUser();
        BeanUtil.copyProperties(userParams, sysUser);
        if (userParams.getPassword() != null) {
            sysUser.setPassword(Md5Util.encrypt(userParams.getPassword()));
        }
        return sysUserMapper.updateByPrimaryKeySelective(sysUser);
    }
}
